﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DALStock
/// </summary>
[System.ComponentModel.DataObject]
public class DALBranch
{
    /// <summary>
    /// Get List of Branch of Post office
    /// </summary>
    /// <returns>List of branch</returns>
    public static List<Branch> ListBranch()
    {
        return DAL.GetDataItems<Branch>(SQL.ViewBranches, new string[] { }, new object[] { });
    }
    
    /// <summary>
    /// Update information of branch
    /// </summary>
    /// <param name="PinCode">The Pincode of branch</param>
    /// <param name="BranchAddress">The branch's address</param>
    /// <param name="BranchName">The branch's name</param>
    /// <param name="BranchFax">The branch's fax</param>
    /// <param name="BranchPhone">The branch's phone</param>
    /// <param name="CityID">The city in which branch belong</param>
    /// <returns>Update successful of not</returns>
    public static bool UpdateBranch(string PinCode,string BranchAddress,string BranchName,string BranchFax,string BranchPhone,string CityID)
    {
        SqlCommand cmd = DAL.CreateCommand();
        cmd.Parameters.AddWithValue("@PinCode",PinCode);
        cmd.Parameters.AddWithValue( "@BranchAddress",BranchAddress);
        cmd.Parameters.AddWithValue( "@BranchName",BranchName);
        cmd.Parameters.AddWithValue( "@BranchFax",BranchFax);
        cmd.Parameters.AddWithValue("@BranchPhone",BranchPhone);
        cmd.Parameters.AddWithValue( "@CityID",CityID);
        cmd.CommandText = SQL.EditBranch;
        if (DAL.ExecuteNonQuery(cmd) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// Add a branch information 
    /// </summary>
    /// <param name="PinCode">The Pincode of branch</param>
    /// <param name="BranchAddress">The branch's address</param>
    /// <param name="BranchName">The branch's name</param>
    /// <param name="BranchFax">The branch's fax</param>
    /// <param name="BranchPhone">The branch's phone</param>
    /// <param name="CityID">The city in which branch belong</param>
    /// <returns>Add successful or not</returns>
    public static bool AddBranch(string PinCode, string BranchAddress, string BranchName, string BranchFax, string BranchPhone, string CityID)
    {
        SqlCommand cmd = DAL.CreateCommand();
        cmd.Parameters.AddWithValue("@PinCode", PinCode);
        cmd.Parameters.AddWithValue("@BranchAddress", BranchAddress);
        cmd.Parameters.AddWithValue("@BranchName", BranchName);
        cmd.Parameters.AddWithValue("@BranchFax", BranchFax);
        cmd.Parameters.AddWithValue("@BranchPhone", BranchPhone);
        cmd.Parameters.AddWithValue("@CityID", CityID);
        cmd.CommandText = SQL.AddBranch;
        if (DAL.ExecuteNonQuery(cmd) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// Delete Branch based on Pincode
    /// </summary>
    /// <param name="PinCode">The branch's pincode</param>
    /// <returns>Delete successful or not</returns>
    public static bool DeleteBranch(string PinCode)
    {
        SqlCommand cmd = DAL.CreateCommand();
        cmd.Parameters.AddWithValue("@PinCode",PinCode);
        cmd.CommandText = SQL.DeleteBranch;
        if (DAL.ExecuteNonQuery(cmd) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// View selected information of specify branch based on Pincode
    /// </summary>
    /// <param name="PinCode"></param>
    /// <returns>The Service entity</returns>
    public static Branch SelectedBranch(string PinCode)
    {
        return DAL.GetDataItem<Branch>(SQL.SelectedBranch, new string[] { "@PinCode" }, new object[] { PinCode });
    }

    /// <summary>
    /// Check selected branch based on PinCode
    /// </summary>
    /// <param name="PinCode"></param>
    /// <returns>True : Continue</returns>
    /// <returns>False : Redirect to ViewBranch</returns>
    public static bool CheckBranch(string PinCode)
    {
        if (DAL.GetDataItem<Branch>(SQL.SelectedBranch, new string[] { "@PinCode" }, new object[] { PinCode }).PinCode == String.Empty)
        {
            return false;
        }
        else
            return true;
    }
}
